/** 
 * All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
 * 
 *MpmProjectBidScoreHisController
 * 评标细则变更历史表
 * 
 * 修改纪录
 * 2022-04-27 版本：1.0 hudeyong 创建。
 * @version 版本：1.0
 * @author 作者：hudeyong
 * 创建日期：2022-04-27
 */

package com.topscomm.mpm.controller;

import java.sql.Clob;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.topscomm.mpm.controller.auto.MpmProjectBidScoreHisControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.JSONUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: hudeyong
 * @date: 2022-04-27
 * @description:评标细则变更历史表Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectBidScoreHis")
public class MpmProjectBidScoreHisController extends MpmProjectBidScoreHisControllerAuto implements IMpmProjectBidScoreHisController {
	/**
	 * @author: hudeyong
	 * @date: 2022-04-27
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_sourceid")) {
			String value = paraMap.get("qry_sourceid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and sourceid='{0}'", value);
		}
		if (paraMap.containsKey("qry_type")) {
			String value = paraMap.get("qry_type").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and type='{0}'", value);
		}
		if (paraMap.containsKey("qry_times")) {
			String value = paraMap.get("qry_times").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and times='{0}'", value);
		}
		return sql.toString();
	}

	/**
	 * @author: hudeyong
	 * @date: 2022-04-27
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}
	
	@Override
	@GetMapping(value = "/list")
	public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			int totalCount = this.service.getCount(sqlWhere);
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
						query.getSidx(), query.getSord());
			} else
				tempList = this.service.queryMapByWhere(sqlWhere);
			// 查询变更历史
//			if (paraMap.containsKey("qry_changerecord")
//					&& ConvertUtil.convertToBoolean(paraMap.get("qry_changerecord"))) {
				StringBufferProxy sql = new StringBufferProxy();
				String sourceid = ConvertUtil.convertToString(paraMap.get("qry_sourceid"));
				String sourcetype = ConvertUtil.convertToString(paraMap.get("qry_sourcetype"));
				sql.appendSingle(
						"select * from basechangelog where sourcetype = '{0}' and sourceid = '{1}' and topsourcetype = '{0}'  and topsourceid = '{1}' AND OPERATETYPE  = 'edit'",
						sourcetype, sourceid);
				List<Map<String, Object>> changelist = this.service.selectExecute(sql.toString());
				if (changelist != null && changelist.size() > 0) {
					// 封装map数据为版本号和变更内容
					Map<Integer, Object> historyMap = new HashMap<Integer, Object>();
					for (Map<String, Object>  map : changelist) {
						String aString = ConvertUtil.clobToString((Clob) map.get("content"));
						List<Map<String, Object>> contentList = (List<Map<String, Object>>) JSONUtil.parseJSONString(aString);
						historyMap.put(ConvertUtil.convertToInt(map.get("version")),contentList);					}
					List<String> changeFieldList = new ArrayList<String>();
					// 对封装的内容进一步处理，封装为版本号和变更的字段code
					for (int key : historyMap.keySet()) {
						changeFieldList = ((List<Map<String, Object>>) historyMap.get(key)).stream()
								.map(map -> (String) map.get("code")).collect(Collectors.toList());
						historyMap.put(key, changeFieldList);
					}
					// 将变更的字段添加到list中
					for (Map<String, Object> temp : tempList) {
						if (historyMap.get(ConvertUtil.convertToInt(temp.get("version"))) == null) {
							String[] emptString = { "" };
							temp.put("changefieldlist", emptString);
						} else {
							temp.put("changefieldlist", historyMap.get(ConvertUtil.convertToInt(temp.get("version"))));
						}
					}
				} else {
					// 如果不存在的话，进行数据填充，防止数据出错
					for (Map<String, Object> temp : tempList) {
						String[] emptString = { "" };
						temp.put("changefieldlist", emptString);
					}
				}
//			}
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

